import allure
import time
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from allure_commons.types import AttachmentType
class BaseAction():

    def __init__(self, driver):
        self.driver = driver
    # 元素定位

    def base_url(self):
        return "https://dev.oms.luguanjia.com"


    def find_element(self, feature, timeout=15, poll=1.0):
        try:
            by = feature[0]
            value = feature[1]
            return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(by, value))
        except :
            return None


    # 多元素定位
    def find_elements(self, feature, timeout=10, poll=1.0):
        try:
            by = feature[0]
            value = feature[1]
            return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(by, value))
        except :
            return None


    # 点击
    def click(self, feature):
        self.find_element(feature).click()

    def click_mouse(self, feature):
        ActionChains(self.driver).click_and_hold(self.find_element(feature)).release().perform()

    # 输入
    def input(self, feature, text):
        self.find_element(feature).clear()
        self.find_element(feature).send_keys(text)

    # 获取元素文本
    def get_text(self, feature):
        return self.find_element(feature).text

    # 获取多元素文本
    def get_texts(self, feature):
        text_list = []
        a = self.find_elements(feature)
        for i in a:
            l = i.text
            text_list.append(l)
        return text_list


    # 获取elert弹框文本
    def get_alert_text(self):
        return self.driver.switch_to.alert().text


    # 通过P标签文本找元素
    @allure.step(title='获取文本{text}')
    def find_by_text_p(self, text):
        ele =  By.XPATH, '//p[text()="{}"]'.format(text)
        return self.find_element(ele).text

    # 通过span标签找文本
    @allure.step(title='获取文本{text}')
    def find_by_text_span(self, text):
        ele =  By.XPATH, '//span[text()="{}"]'.format(text)
        return self.find_element(ele).text

    # 通过div标签找文本
    @allure.step(title='获取文本{text}')
    def find_by_text_div( self, text ):
        ele = By.XPATH, '//div[text()="{}"]'.format(text)
        return self.find_element(ele).text

    # 通过cite标签找文本
    @allure.step(title='获取文本{text}')
    def find_by_text_cite( self, text ):
        ele = By.XPATH, '//cite[text()="{}"]'.format(text)
        return self.find_element(ele).text

    # 通过p标签找文本
    @allure.step(title='获取文本{text}')
    def find_by_p_cite(self, text):
        time.sleep(2)
        ele = By.XPATH, '//p[text()="{}"]'.format(text)
        return self.find_element(ele).text

    def find_by_class_num(self, text, n):
        ele = By.CLASS_NAME, '{}'.format(text)
        return self.find_elements(ele)[n]

    @allure.step(title='获取文本：{text}')
    def find_span_contains_text(self, text):
        ele = By.XPATH, '//span[contains(text(),"{}")]'.format(text)
        return self.get_text(ele)


    # 模糊匹配文本'//a[contains(text(), "退出")]')
    @allure.step(title='点击{text}')
    def click_span_contains_text(self, text):
        ele = By.XPATH, '//button//span[contains(text(),"{}")]'.format(text)
        return self.click(ele)

    @allure.step(title='点击{text}')
    def click_label_span_contains_text(self, text):
        ele = By.XPATH, '//label//span[contains(text(),"{}")]'.format(text)
        return self.click(ele)

    # 点击span文本
    @allure.step(title='点击{text}')
    def click_span_text(self, text):
        ele = By.XPATH, '//span[text()="{}"]'.format(text)
        time.sleep(0.5)
        return self.click(ele)

    @allure.step(title='点击{text}')
    def click_span_text_mouse(self, text):
        ele = By.XPATH, '//span[text()="{}"]'.format(text)
        time.sleep(0.5)
        return ActionChains(self.driver).click_and_hold(self.find_element(ele)).release().perform()

    @allure.step(title='点击{text}')
    def click_span_contains_text_mouse(self, text):
        ele = By.XPATH, '//button//span[contains(text(),"{}")]'.format(text)
        time.sleep(0.5)
        return ActionChains(self.driver).click_and_hold(self.find_element(ele)).release().perform()

    # 点击div文本
    @allure.step(title='点击{text}')
    def click_div_text( self, text ):
        ele = By.XPATH, '//div[text()="{}"]'.format(text)
        return self.click(ele)

    # 点击link_text文本
    @allure.step(title='点击{text}')
    def click_link_text( self, text ):
        ele = By.LINK_TEXT, '{}'.format(text)
        return self.click(ele)

    # 点击li文本
    @allure.step(title='点击{text}')
    def click_li_text(self, text):
        ele = By.XPATH, '//li[text()="{}"]'.format(text)
        return self.click(ele)

    @allure.step(title='点击{text}')
    def click_button_text(self, text):
        ele = By.XPATH, '//button[text()="{}"]'.format(text)
        return self.click(ele)

    @allure.step(title='输入框{text}输入:{name}')
    def input_placeholder_text(self, text, name):
        ele = By.XPATH, '//input[@placeholder="{}"]'.format(text)
        self.find_element(ele).clear()
        return self.find_element(ele).send_keys(name)

    @allure.step(title='输入框{text}输入:{name}')
    def input_span_brother_text(self, text, name):
        ele = By.XPATH, '//div//span[text()="{}"]/..//input'.format(text)
        self.find_element(ele).clear()
        return self.find_element(ele).send_keys(name)

    @allure.step(title='下拉框选择{text}')
    def select_text(self, text):
        ele = By.XPATH, '//input[@placeholder="请选择"]'
        self.find_element(ele).click()
        ele = By.XPATH, '//span[text()="{}"]'.format(text)
        return self.find_element(ele).click()

    @allure.step(title='下拉框:{name} 选择:{text}')
    def select_name_text(self, name,text):
        ele = By.XPATH, '//input[@placeholder="{}"]'.format(name)
        self.find_element(ele).click()
        ele = By.XPATH, '//span[text()="{}"]'.format(text)
        time.sleep(0.5)
        return self.find_element(ele).click()


    @allure.step(title='选择日期时间')
    def input_day_time(self):
        # allure.attach( "选择日期时间", "选择日期" )
        ele = By.XPATH, '//input[@placeholder="选择日期"]'
        self.find_element(ele).click()
        time.sleep(0.5)
        ele2 = By.XPATH, "//td[@class='available today']//div//span"
        self.find_element(ele2).click()
        ele3 = By.XPATH, '//input[@placeholder="选择时间"]'
        self.find_element(ele3).click()
        time.sleep(0.5)
        self.click_button_text("确定")

    def loading(self):
        i = 0
        while True:
            if i == 60:
                return
            try:
                i += 1
                ele = WebDriverWait(self.driver, 1).until(lambda x: x.find_element(By.XPATH, '//p[text()="拼命加载中"]'))
                ele.click()
            except TimeoutException:
                return


    # 向下滑动
    def scroll_dwon(self, num=1, lenth=2000):
        """
        :param num: 滑动之后再滑动多少次,每次+1000
        :param lenth: 第一次滑动的位置
        :return:
        """
        for i in range(num):
            js = 'window.scrollTo(0, {})'.format(lenth)
            self.driver.execute_script(js)
            lenth += 1000
            time.sleep(5)


    def screenshot_as_png(self):
        allure.attach(self.driver.get_screenshot_as_png(), name="Screenshot", attachment_type=AttachmentType.PNG)